<?php
class AMS_Core_Controller_Request {
    protected $config = null;
    protected $controller;
    protected $controllerName = null;
    protected $actionName = null;
    protected $stage = 'frontend';
    protected $_get = null;
    protected $_post = null;
    
    public function __construct($config) {
        $this->config = $config;
        $process = preg_replace('/^\\'.$_SERVER['SCRIPT_NAME'].'\\//i','',$_SERVER['REQUEST_URI']);
        if (strpos($process,'/')===false) $process.='/';
        $request = explode('/',$process);
        if (isset($request[0])) {
            $this->controllerName = $request[0];
            unset($request[0]);
        }
        if (isset($request[1]) && $request[1]) {
            $actionName = $request[1];
            $this->actionName = $request[1];
            unset($request[1]);
        }else $this->actionName='index';
        if (!$this->controllerName || !isset($this->config['routers'][$this->controllerName]))
            $this->controllerName = 'page';
        $className = $this->config['routers'][$this->controllerName]['module'] . '_' . ucfirst($this->controllerName) . 'Controller';
        $this->controller = new $className();
        $this->controller->setName($this->controllerName)->setAction($this->actionName);
        foreach ($request as $key=>$value) {
            if ($value && (!($key & 1))) {
                if (isset($request[$key+1])) $params[$value] = $request[$key+1];
                else $params[$value] = null;
            }
        }
        if (isset($params))
            $this->_get = $params;
    }
    
    public function setStage($stageName) {
        $this->stage = $stageName;
        return $this;
    }
    public function getStage() {
        return $this->stage;
    }
    public function getParam($key) {
        if (isset($this->_get[$key])) return $this->_get[$key];
    }
    public function getController() {
        return $this->controller;
    }
    public function getAction() {
        return $this->actionName;
    }
}

?>
